﻿using System.Collections.Generic;
using System.Linq;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.Data.Database.Repository.RepositoryInterfaces;

namespace KLS.PPDS.Data.Database.Repository.EntityRepository
{
    public class ArticleRepository:LinqRepository<Article>,IArticleRepository
    {
        public IQueryable<Article> GetAll() 
        {
            return FindAll().Where(c => c.IsDeleted.Equals(false));
        }

        public List<Article> GetAllArticle()
        {
            return GetAll().ToList();

        }

        public Article GetArticleById(int articleId)
        {
            return FindOne(articleId);

        }

        public List<Article> GetArticleByPublicationId(int publicationId)
        {

            return FindAll().Where(x => x.PublicationId.Equals(publicationId)).ToList();
        }

        public bool IsOccupied(int pubId, int articleId, double startPage, double endPage) 
        {

            //startPage = 0;
            //endPage = 0;
            var result = false;
            ////var x = GetAll().Where(c => c.PublicationId.Equals(pubId) && ((endPage > c.StartPage && endPage < c.EndPage) || (startPage >= c.StartPage && startPage < c.EndPage))).ToList();

            ////if (x != null && x.Count > 0) 
            ////{

            ////    foreach (var item in x)
            ////    {
            ////        if (item.Id != articleId && pubId == item.PublicationId)
            ////        {
            ////            result = true;
            ////        }
            ////    }
                
                
            ////    return result;
            ////}

            // inside range
            var x = GetAll().Where(c => c.PublicationId.Equals(pubId) && (c.StartPage> startPage && c.EndPage <= endPage)).ToList();
            if (x != null && x.Count > 0) 
            {
                foreach (var item in x)
                {
                    if (item.Id != articleId && pubId == item.PublicationId)
                    {
                        result = true;
                    }
                }
                
            }

            return result;
        }

        //this for start page list
        public bool IsOccupied(int pubId,  double startPage, double endPage)
        {

            //startPage = 0;
            //endPage = 0;
            var result = false;
            var x = GetAll().Where(c => c.PublicationId.Equals(pubId) && ((endPage > c.StartPage && endPage < c.EndPage) || (startPage >= c.StartPage && startPage < c.EndPage))).ToList();

            if (x != null && x.Count > 0)
            {
                result = true;
                return result;
            }

            // inside range
            x = GetAll().Where(c => c.PublicationId.Equals(pubId) && c.StartPage > startPage && c.EndPage < endPage).ToList();
            if (x != null && x.Count > 0)
            {
                result = true;
            }

            return result;
        }


        public bool IsOccupied(int pubId, int articleId, double pageNo) 
        {
            var result = false;
            var x = GetAll().Where(c => c.PublicationId.Equals(pubId) && ((pageNo >= c.StartPage && pageNo < c.EndPage))).ToList();
            if (x != null && x.Count > 0)
            {
                //return true;
                foreach (var item in x) 
                {
                    if (item.Id != articleId && pubId == item.PublicationId) 
                    {
                        result = true;
                    }
                }
                                
            }
            return result;
        }

        public bool IsEndPageOccupied(int pubId, int articleId, double pageNo)
        {
            var result = false;
            var x = GetAll().Where(c => c.PublicationId.Equals(pubId) && ((pageNo > c.StartPage && pageNo <= c.EndPage))).ToList();
            if (x != null && x.Count > 0)
            {
                foreach (var item in x)
                {
                    if (item.Id != articleId)
                    {
                        result = true;
                    }
                }
            }
            return result;
        }

        public List<Article> GetArticleByPublicationIdAndStartPage(int pubId, int artId,  int pageNo) 
        {
           // var x = GetAll().Where(c => c.PublicationId.Equals(pubId) && c.StartPage>startPage )

            //return GetAll().Where(c => c.PublicationId.Equals(pubId) && ((c.StartPage >= startPage && c.StartPage < startPage + 1) || (c.EndPage >= startPage && c.EndPage < startPage + 1))).ToList();
            var x = GetAll().Where(c => c.PublicationId.Equals(pubId) && c.Id != artId && ((c.EndPage >= pageNo && c.EndPage < pageNo + 1))).OrderByDescending(c => c.EndPage).ToList();
            return x;
        }




        public List<Article> GetArticleByPageNo(int pubId, int pageNo) 
        {
            var startPage = pageNo;
            var endPage = startPage + 1;
            return GetAll().Where(c => c.PublicationId.Equals(pubId) && ((endPage > c.StartPage && endPage <= c.EndPage) 
                ||(startPage >= c.StartPage && startPage < c.EndPage) 
                || (c.StartPage > startPage && c.EndPage < endPage))).OrderBy(x=>x.StartPage).ToList();
        }


        public List<Article> GetAllStandByArticle(int publicationId)
        {
            return GetAll().Where(x => x.PublicationId.Equals(publicationId) && x.StartPage == null).ToList();
        }
    }
}
